package com.ruoyi.project.zxbd.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.zxbd.service.impl.BdGoodsAttrValueServiceImpl;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.common.zx.PageData;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 商品属性Controller
 *
 * @author zx
 * @date 2023-05-26
 */
@RestController
@RequestMapping("/zxbd/value")
public class BdGoodsAttrValueController extends BaseController {
    @Autowired
    private  BdGoodsAttrValueServiceImpl bdGoodsAttrValueService;

    /**
     * 查询商品属性列表
     */
    @PreAuthorize("@ss.hasPermi('zxbd:value:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = bdGoodsAttrValueService.selectBdGoodsAttrValueList(pd);
        return getDataTable(list);
    }

    /**
     * 导出商品属性列表
     */
    @PreAuthorize("@ss.hasPermi('zxbd:value:export')")
    @Log(title = "商品属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageData pd) {
        List<PageData> list = bdGoodsAttrValueService.selectBdGoodsAttrValueList(pd);
        ExcelUtil<PageData> util = new ExcelUtil<PageData>(PageData.class);
        util.exportExcel(response, list, "商品属性数据");
    }

    /**
     * 获取商品属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('zxbd:value:query')")
    @GetMapping(value = "/{attribute_name}/{attribute_value}")
    public AjaxResult getInfo(@PathVariable("attribute_name") String attribute_name ,@PathVariable("attribute_value") String attribute_value) {
        return success(bdGoodsAttrValueService.selectBdGoodsAttrValueByAttribute_name(attribute_name,attribute_value));
    }

    /**
     * 新增商品属性
     */
    @PreAuthorize("@ss.hasPermi('zxbd:value:add')")
    @Log(title = "商品属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageData pd) {
        int i = 0;
        try {
            i = bdGoodsAttrValueService.insertBdGoodsAttrValue(pd);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                logger.error("异常{}",e.getMessage());
                return AjaxResult.error(90003, "属性名和属性值已存在");
            }
            logger.error("异常{}",e.getMessage());
        }
        return toAjax(i);
    }

    /**
     * 修改商品属性
     */
    @PreAuthorize("@ss.hasPermi('zxbd:value:edit')")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageData pd) {
        int i = 0;
        try {
            i = bdGoodsAttrValueService.updateBdGoodsAttrValue(pd);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                logger.error("异常{}",e.getMessage());
                return AjaxResult.error(90003, "属性名和属性值已存在");
            }
            logger.error("异常{}",e.getMessage());
        }
        return toAjax(i);
    }

    /**
     * 删除商品属性
     */
    @PreAuthorize("@ss.hasPermi('zxbd:value:remove')")
    @Log(title = "商品属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attribute_names}/{attribute_value}")
    public AjaxResult remove(@PathVariable("attribute_names") String attribute_names,@PathVariable("attribute_value") String attribute_value) {
        return toAjax(bdGoodsAttrValueService.deleteBdGoodsAttrValueByAttribute_name(attribute_names,attribute_value));
    }
}
